# vim: ai ts=2 sts=2 et sw=2

try:
  import sys
  import random
  import math
  import os
  import getopt
  import pygame
  import ConfigParser

  from socket import *
  from pygame.locals import *

  import utils
  import layeredtilemap
  
except ImportError, message:
  print "Could not load module %s" % (message)
  sys.exit(2)

SCREEN_HEIGHT = 320
SCREEN_WIDTH = 320

WINDOW_TITLE = "pygame skeleton"

class Pointer():
  def __init__(self, imageName):
    pygame.mouse.set_visible(False)
    
    self.image = utils.load_image(imageName)
    self.rect = self.image.get_rect()
    self.rect.topleft = (0, 0)

  def update(self):
    self.rect.topleft = pygame.mouse.get_pos()

  def draw(self, screen):
    screen.blit(self.image, self.rect)

class Unit():
  def __init__(self, imageName):
    self.image = utils.load_image(imageName)
    self.rect = self.image.get_rect()
    self.rect.topleft = (0, 0)
  
  def update(self):
    pass

  def draw(self, screen):
    screen.blit(self.image, self.rect)

def load_config():
  config_file_name = "config.ini"
  config = ConfigParser.SafeConfigParser()
  config.read(config_file_name)
  return config

def main():
  pygame.init()

  config = load_config()

  screen = pygame.display.set_mode((config.getint("ui", "width"), config.getint("ui", "height")))
  pygame.display.set_caption(WINDOW_TITLE)

  pointer = Pointer("pointer_standard.png")
  font = pygame.font.Font(os.path.join("data", "fonts", config.get("ui", "font")), 12)
  clock = pygame.time.Clock()

  tileMap = layeredtilemap.LayeredTileMap()
  tileMap.load_map_xml(os.path.join("data", "maps", "layeredfield.xml"))
  tileView = layeredtilemap.LayeredTileMapView(tileMap, 10, 10, 10, 10)

  running = True
  while running:
    # tick the clock and try to maintain 60 fps
    clock.tick(60)
    
    # event loop
    for event in pygame.event.get():
      if event.type == QUIT:
        running = False

    # update our mouse pointer sprite
    pointer.update()

    # get our FPS and put it into a surface so we can write it
    # to the screen
    fpsMessage = "FPS: %s" % (round(clock.get_fps(), 2))
    fpsMessageSurface = font.render(fpsMessage, 1, (255, 255, 255))

    screen.fill((100, 100, 100))
    tileView.draw(screen)
    screen.blit(fpsMessageSurface, (0, 0))
    pointer.draw(screen)
    pygame.display.flip()

  pygame.quit()

if __name__ == '__main__':
  main()
